package com.jkl.gatewayserver.Web.Controller;

import com.jkl.entity.*;
import com.jkl.gatewayserver.Web.Service.AppService;
import com.jkl.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class AppController {
    private final AppService appService;

    @Autowired
    public AppController(AppService appService) {
        this.appService = appService;
    }

    //APP应用登录
    @PostMapping("/App/login")
    @ResponseBody
    public RAppLoginForm login(@RequestBody AppLoginForm appLoginForm) throws Exception {
        return appService.AppVerify(appLoginForm);
    }

    //获取用户头像
    @GetMapping("/App/img")
    public void getSelfImgRes(@RequestParam String name, @RequestParam String email, @RequestParam boolean temporary, HttpServletResponse response) throws Exception {
        String targetFile;
        if ("get_head_img".equals(name)) {//获取用户头像
            targetFile = "/userRes/" + email + "/head_img/head.png";
            appService.downloadFile(targetFile, response);
        }
    }

    //文件上传
    @PostMapping("/App/upload")
    public ResponseEntity<List<String>> uploadFile(@RequestParam Map<String, MultipartFile> files,@RequestParam String type ,@RequestParam String email) {
        String savePath;
        if("image".equals(type)){
            savePath="/userRes/"+email+"/temporary/imageRes";
        }
        else {
            savePath="/userRes/"+email+"/temporary/fileRes";
        }
//        return uploadServer.upLoadFile(savePath,files);
        return appService.upLoadFile(savePath,files);
    }
    //文件下载
    @PostMapping("/App/download")
    public void downloadFile(@RequestParam String type,@RequestParam String email,@RequestParam String fileName ,@RequestParam String suffix,HttpServletResponse response) throws Exception {
        String targetPath;
        if(("image".equals(type))&&!suffix.isEmpty()){
            targetPath="/userRes/"+email+"/temporary/imageRes/"+fileName+"."+suffix;
        }
        else{
            if(suffix.isEmpty())
                targetPath="/userRes/"+email+"/temporary/fileRes/"+fileName;
            else
                targetPath="/userRes/"+email+"/temporary/fileRes/"+fileName+"."+suffix;
        }
//        downloadServer.downloadFile(targetPath, response);
        appService.downloadFile(targetPath, response);
    }

    @PostMapping("/App/messages")
    @ResponseBody
    public List<Message> getMessages(@RequestParam String email, @RequestParam Long lastTime){
        if(lastTime!=0)
            return appService.getUnreceivedMessages(email,lastTime);
        else
            return appService.getAllMessages(email);
    }

    //获取好友信息
    @PostMapping("/App/friends")
    @ResponseBody
    public List<AppFriend> getFriend(@RequestParam String email){
        return appService.getFriends(email);
    }

    @PostMapping("/App/search")
    @ResponseBody
    public List<Account> searchFriend(@RequestParam String email, @RequestParam String text){
        return appService.findFriend(email,text);
    }
    //获取好友申请列表
    @PostMapping("/App/getApplication")//查询未确认的好友
    @ResponseBody
    public List<SCApplication> getApplication(@RequestParam String email){
        return appService.getApplication(email);
    }

    //发送消息
    @PostMapping("/App/sendMessage")
    public Mono<ResponseEntity<RMessage>> sendMessage(@RequestBody Message msg){
//        msg.setCreateTime(System.currentTimeMillis());
        return appService.sendMessage(msg)
                .map(ResponseEntity::ok)
                .onErrorResume(e -> Mono.just(ResponseEntity.status(HttpStatus.OK).body(new RMessage(msg))))
                .defaultIfEmpty(ResponseEntity.notFound().build());
    }

    //发出好友申请
    @PostMapping("/App/buildRelationship")
    public Mono<ResponseEntity<RCSApplication>> buildRelationship(@RequestBody CSApplication application) {
        application.setBuildTime(System.currentTimeMillis());
        //验证申请是否存在
        return  appService.buildRelationship(application)
                .map(ResponseEntity::ok)
                .onErrorResume(e -> Mono.just(ResponseEntity.status(HttpStatus.OK).body(new RCSApplication(application))))
                .defaultIfEmpty(ResponseEntity.notFound().build());
    }

    //确认好友申请
    @PostMapping("/App/confirmRelationship")
    public Mono<ResponseEntity<RCSAgree>> confirmRelationship(@RequestBody CSAgree agree) {
        agree.setBuildTime(System.currentTimeMillis());
        return appService.confirmRelationship(agree)
                .map(ResponseEntity::ok)
                .onErrorResume(e -> Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null)))
                .defaultIfEmpty(ResponseEntity.notFound().build()); // 注意：这可能不是您想要的，因为通常404表示资源未找到，而不是操作失败
    }
    @PostMapping("/App/deleteRelationship")
    public  void deleteRelationship(){

    }
    @GetMapping("/")
    public String test(){
        return "test";
    }
}
